package com.tt.job.api.schedule;

import java.io.Serializable;
import java.util.Map;

/**
 * 任务调度信息
 */
public class JobSchedule implements Serializable {
    private String name;
    private String group;
    private int retryDelayLevel;
    private boolean enabled;
    private ScheduleType type;
    private String jobClass;
    private Map<String, String> jobParameters;
    private String key = "";

    /**
     * 消息进行重试消费时，指定重试的延迟时间级别
     * RocketMQ的消息延迟级别为：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     * 级别从1开始，如级别为3表示延迟10s执行
     * 默认为0表示按照mq的默认延迟级别进行重试消费
     * @return
     */
    public int getRetryDelayLevel() {
        return retryDelayLevel;
    }

    public void setRetryDelayLevel(int retryDelayLevel) {
        this.retryDelayLevel = retryDelayLevel;
    }

    /**
     * 是否启用该调度
     * @return
     */
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * 当前调度的类型
     * @return
     */
    public ScheduleType getType() {
        return type;
    }

    public void setType(ScheduleType type) {
        this.type = type;
    }

    /**
     * 调度的名称
     * @return
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 调度的分组
     * @return
     */
    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    /**
     * 所需要调度的job的class全称
     * @return
     */
    public String getJobClass() {
        return jobClass;
    }

    public void setJobClass(String jobClass) {
        this.jobClass = jobClass;
    }

    /**
     * 此调度给任务分配的参数数据
     * @return
     */
    public Map<String, String> getJobParameters() {
        return jobParameters;
    }

    public void setJobParameters(Map<String, String> jobParameters) {
        this.jobParameters = jobParameters;
    }

    /**
     * 调度涉及的对象的唯一标识，方便对同一对象的操作按顺序进行
     * @return
     */
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    /**
     * schedule中type，group，name不变，使用这些来生成ha'shCode方便集合存储
     * @return
     */
    @Override
    public int hashCode() {
        return type.hashCode() + name.hashCode() * 31 + group.hashCode() * 62;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof JobSchedule){
            JobSchedule schedule = (JobSchedule) obj;
            return this.type.equals(schedule.type)
                    && group.equals(schedule.group)
                    && name.equals(schedule.name);
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("group:");
        builder.append(getGroup());
        builder.append(", name:");
        builder.append(getName());
        builder.append(", type:");
        builder.append(getType());
        builder.append(", enabled:");
        builder.append(isEnabled());
        builder.append(", retryDelayLevel:");
        builder.append(getRetryDelayLevel());
        builder.append("}");

        return builder.toString();
    }
}
